//
//  CashbackRecordFVC.m
//  NewPro
//
//  Created by 何荧红 on 2019/6/12.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "CashbackRecordFVC.h"
#import "CashbackRecordFCell.h"

#import "CashbackRecordStore.h"
#import "CashbackRecordModel.h"

@interface CashbackRecordFVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, assign) NSInteger current;
@property (nonatomic, assign) NSInteger totalCurrent;

@end

@implementation CashbackRecordFVC

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    return _dataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCSS];
    [self loadData];
}
- (void)initCSS{
    self.current = 1;
    self.totalCurrent = 1;
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.sectionFooterHeight = 1;
    self.tableView.sectionHeaderHeight = 0.01f;
    [self.tableView registerNib:[UINib nibWithNibName:@"CashbackRecordFCell" bundle:nil] forCellReuseIdentifier:@"CashbackRecordFCellID"];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.current = 1;
        self.dataArr = [[NSMutableArray alloc]init];
        [self loadData];
    }];
    self.tableView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
        self.current += 1;
        if (self.current <= self.totalCurrent) {
            [self loadData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
    }];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(10);
    }];
}
- (void)loadData{
    [[[CashbackRecordStore alloc]init] GetListWithSign:@"0" AndCurrent:self.current Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            self.totalCurrent = [[NSString stringWithFormat:@"%@",res[@"data"][@"pages"]] integerValue];
            NSArray *arr = res[@"data"][@"records"];
            if (arr.count > 0) {
                for (NSDictionary *dic in arr) {
                    CashbackRecordModel *model = [CashbackRecordModel mj_objectWithKeyValues:dic];
                    model.uid = [NSString stringWithFormat:@"%@",dic[@"id"]];
                    model.backAmount = [NSString stringWithFormat:@"%@",dic[@"newAmount"]];
                    [self.dataArr addObject:model];
                }
            }else{
                [self showMBPError:@"暂无数据"];
            }
        }else{
            [self showMBPError:res[@"msg"]];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } Failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 85;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *foot = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
    foot.backgroundColor = [UIColor colorWithHexString:@"E6E6E6"];
    return foot;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CashbackRecordFCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"CashbackRecordFCellID"];
    if (self.dataArr.count > 0) {
        Cell.model = self.dataArr[indexPath.section];
    }
    Cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return Cell;
}

@end
